<template>
  	<div class="login_page fillcontain">
	  	<transition name="form-fade" mode="in-out">
	  		<section class="form_contianer" v-show="showLogin">
		  		<div class="manage_tip">
		  			<p>企信部留言板</p>
		  		</div>
		    	<el-form :model="loginForm" :rules="rules" ref="loginForm">
					<el-form-item prop="username">
						<el-input v-model="loginForm.username" placeholder="用户名"><span>dsfsf</span></el-input>
					</el-form-item>
					<el-form-item prop="password">
						<el-input type="password" placeholder="密码" v-model="loginForm.password"></el-input>
					</el-form-item>
					<el-form-item>
				    	<el-button type="primary" @click="submitForm('loginForm')" class="submit_btn">登陆</el-button>
				  	</el-form-item>
				  	<el-form-item>
                        <el-button type="primary" @click="register('loginForm')" class="submit_btn">注册</el-button>
                    </el-form-item>
				</el-form>

	  		</section>
	  	</transition>
  	</div>
</template>

<script>

	import VueResource from 'vue-resource'

	export default {
          data(){
          			return {
          				loginForm: {
          					username: '',
          					password: '',
          				},
          				rules: {
          					username: [
          			            { required: true, message: '请输入用户名', trigger: 'blur' },
          			        ],
          					password: [
          						{ required: true, message: '请输入密码', trigger: 'blur' }
          					],
          				},
          				showLogin: true,
          			}
          		},


          http: {
            headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
          },
          methods: {

            submitForm: function (formName) {
              var _this = this;
              var userLoginReqDto = new Object();
              userLoginReqDto.userName = _this.loginForm.username;
              userLoginReqDto.userPassword =  _this.loginForm.password;
              console.log(_this.loginForm.username,  _this.loginForm.password)
              _this.$http.post('http://localhost:8087/user/login',
                    userLoginReqDto
              ,{emulateJSON:true}
              )
                .then(function (response) {
                  console.log("登录成功");
                  var result = response.data;
                  console.log(result);
                  if (result.status == "0") {

                    localStorage.setItem('user',JSON.stringify(result.data));
                    _this.$router.push(
                      { path: '/message'
                      });
                  } else {
                    this.$notify.error({
                    	title: '错误',
                    	message: '请输入正确的用户名密码',
                    	offset: 100
                    	});
                    return false;
                  }
                })
                .catch(function (error) {
                  console.log(error);
                });
            },
            register: function (formName) {
                          var _this = this;
                          var userRegisterReqDto = new Object();
                          userRegisterReqDto.userName = _this.loginForm.username;
                          userRegisterReqDto.userPassword =  _this.loginForm.password;
                          console.log(_this.loginForm.username,  _this.loginForm.password)
                          _this.$http.post('http://localhost:8087/user/register',
                                userRegisterReqDto
                          ,{emulateJSON:true}
                          )
                            .then(function (response) {

                              var result = response.data;
                              console.log(result);
                              if (result.status == "0") {
                                localStorage.setItem('user',JSON.stringify(result.data));
                                console.log("注册成功");
                                _this.$router.push(
                                  { path: '/message'
                                  });
                              } else {
                                this.$notify.error({
                                	title: '错误',
                                	message: '注册失败',
                                	offset: 100
                                	});
                                return false;
                              }
                            })
                            .catch(function (error) {
                              console.log(error);
                            });
                        }

          }
        }
</script>

<style lang="less" scoped>
	@import '../style/mixin';
	.login_page{
		background-color: #324057;
	}
	.manage_tip{
		position: absolute;
		width: 100%;
		top: -100px;
		left: 0;
		p{
			font-size: 34px;
			color: #fff;
		}
	}
	.form_contianer{
		.wh(320px, 210px);
		.ctp(320px, 210px);
		padding: 25px;
		border-radius: 5px;
		text-align: center;
		background-color: #fff;
		.submit_btn{
			width: 100%;
			font-size: 16px;
		}
	}
	.tip{
		font-size: 12px;
		color: red;
	}
	.form-fade-enter-active, .form-fade-leave-active {
	  	transition: all 1s;
	}
	.form-fade-enter, .form-fade-leave-active {
	  	transform: translate3d(0, -50px, 0);
	  	opacity: 0;
	}
</style>
